﻿using YiNing.UI.Config;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace YiNing.UI.Controls
{
    public class DarkSectionPanel : Panel
    {
        public enum MouseDirection
        {
            East,
            West,
            South,
            North,
            Southeast,
            Southwest,
            Northeast,
            Northwest,
            None
        }
        
        #region Field Region
        private Point startPoint;
        private bool _dragEnable = false;
        private string _sectionHeader;
        private Size oldSize;
        private MouseDirection direction;
        #endregion

        #region Property Region

        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Padding Padding
        {
            get { return base.Padding; }
        }

        [Category("Appearance")]
        [Description("The section header text associated with this control.")]
        public string SectionHeader
        {
            get { return _sectionHeader; }
            set
            {
                _sectionHeader = value;
                Invalidate();
            }
        }

        //public new DockStyle Dock
        //{
        //    get { return Dock; }
        //    set
        //    {
        //        Dock = value;
            
        //        Invalidate();
        //    }
        //}


        [Description("是否可以拖动")]
        [DefaultValue(false)]
        public bool DragEnable
        {
            get { return _dragEnable; }
            set
            {
                if (Dock == DockStyle.None)
                {
                    _dragEnable = value;
                    Invalidate();
                }
                else
                {
                    throw new System.Exception("Dock属性只有是None的时候才可以拖动");
                }
            }
        }

        #endregion

        #region Constructor Region

        public DarkSectionPanel()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.ResizeRedraw |
                     ControlStyles.UserPaint, true);

            base.Padding = new Padding(1, 25, 1, 1);
        }

        #endregion

        #region Event Handler Region

        protected override void OnEnter(System.EventArgs e)
        {
            base.OnEnter(e);

            
            Invalidate();
        }

        protected override void OnLeave(System.EventArgs e)
        {
            base.OnLeave(e);

            Invalidate();
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (Controls.Count > 0)
                Controls[0].Focus();
            startPoint.X = e.X;
            startPoint.Y = e.Y;

            if (e.Button == MouseButtons.Left)
            {
                oldSize = Size;
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            //if (!_dragEnable) return;

            if (_dragEnable)
            {
                var bgRect = new Rectangle(0, 0, ClientRectangle.Width, 25);
                if (bgRect.Contains(new Point(e.X, e.Y)))
                {
                    Cursor.Current = Cursors.NoMove2D;
                }
                else
                {
                    Cursor.Current = Cursors.Default;
                }
                if (e.Button == MouseButtons.Left)
                {
                    Point mousePositon = Control.MousePosition;
                    mousePositon.Offset(-startPoint.X, -startPoint.Y);
                    Point point = Parent.PointToClient(mousePositon);
                    if (point.X < 0) point.X = 0;
                    if (point.Y < 0) point.Y = 0;
                    if (point.X + Width > Parent.Width) point.X = Parent.Width - Width;
                    if (point.Y + Height > Parent.Height) point.Y = Parent.Height - Height;
                    Location = point;
                }
            }
   

            //if (e.Location.X >= Width - 5 && e.Location.Y >= Height - 5)
            //{
            //    Cursor = Cursors.SizeNWSE;
            //    direction = MouseDirection.Southeast;
            //}
            //else if (e.Location.X <= 5 && e.Location.Y <= 5)
            //{
            //    Cursor = Cursors.SizeNWSE;
            //    direction = MouseDirection.Northwest;
            //}
            //else if (e.Location.X <= 5 && e.Location.Y >= Height - 5)
            //{
            //    Cursor = Cursors.SizeNESW;
            //    direction = MouseDirection.Southwest;
            //}
            //else if (e.Location.X >= Width - 5 && e.Location.Y <= 5)
            //{
            //    Cursor = Cursors.SizeNESW;
            //    direction = MouseDirection.Northeast;
            //}
            //else
            //{
            //    Cursor = Cursors.Default;
            //}
            Invalidate();
        }


        #endregion

        #region Paint Region

        protected override void OnPaint(PaintEventArgs e)
        {
            // 使用双缓冲
            this.DoubleBuffered = true;
            // 背景重绘移动到此
            if (this.BackgroundImage != null)
            {
                /* 创建一个白色背景的位图作为背景，防止点击伸缩时重绘背景导致闪烁。
              * 不可在初始化的时候绘制该背景位图，
              * 因为容器自适应于父控件的长宽，初始化时候的长宽不一定是最终长宽。
              * 所以每次触发绘制方法的时候都要重新读取控件的长宽以便重绘位图
              */
                Bitmap bmp = new Bitmap(this.Width, this.Height);
                using (Graphics graph = Graphics.FromImage(bmp))
                {
                    Rectangle ImageSize = new Rectangle(0, 0, this.Width, this.Height);
                    graph.FillRectangle(Brushes.White, ImageSize);
                }

                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                e.Graphics.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, this.Width, this.Height),
                    0, 0, this.Width, this.Height,
                    System.Drawing.GraphicsUnit.Pixel);
            }
            else
            {
                var g = e.Graphics;
                var rect = ClientRectangle;

                // Fill body
                using (var b = new SolidBrush(Colors.GreyBackground))
                {
                    g.FillRectangle(b, rect);
                }

                // Draw header
                var bgColor = ContainsFocus ? Colors.BlueBackground : Colors.HeaderBackground;
                var darkColor = ContainsFocus ? Colors.DarkBlueBorder : Colors.DarkBorder;
                var lightColor = ContainsFocus ? Colors.LightBlueBorder : Colors.LightBorder;

                using (var b = new SolidBrush(bgColor))
                {
                    var bgRect = new Rectangle(0, 0, rect.Width, 25);
                    g.FillRectangle(b, bgRect);
                }

                using (var p = new Pen(darkColor))
                {
                    g.DrawLine(p, rect.Left, 0, rect.Right, 0);
                    g.DrawLine(p, rect.Left, 25 - 1, rect.Right, 25 - 1);
                }

                using (var p = new Pen(lightColor))
                {
                    g.DrawLine(p, rect.Left, 1, rect.Right, 1);
                }

                var xOffset = 3;

                using (var b = new SolidBrush(Colors.LightText))
                {
                    var textRect = new Rectangle(xOffset, 0, rect.Width - 4 - xOffset, 25);

                    var format = new StringFormat
                    {
                        Alignment = StringAlignment.Near,
                        LineAlignment = StringAlignment.Center,
                        FormatFlags = StringFormatFlags.NoWrap,
                        Trimming = StringTrimming.EllipsisCharacter
                    };

                    g.DrawString(SectionHeader, Font, b, textRect, format);
                }

                // Draw border
                using (var p = new Pen(Colors.DarkBorder, 1))
                {
                    var modRect = new Rectangle(rect.Left, rect.Top, rect.Width - 1, rect.Height - 1);

                    g.DrawRectangle(p, modRect);
                }
            }
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            return;
            // Absorb event
        }

        #endregion
    }
}
